package timo.rabbitmq.example.producer.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import timo.rabbitmq.example.producer.service.OrderService;

/**
 * @author timo
 * @date 2022/7/12
 */
@Service
public class OrderServiceImpl implements OrderService {
    //RabbitAutoConfiguration中创建了AmpqTemplate实例
    @Autowired
    AmqpTemplate amqpTemplate;

    @Override
    public String saveOrder(JSONObject jsonObject)  {
        String orderId = IdUtil.getSnowflake(0,0).nextIdStr();
        jsonObject.set("id",orderId);
        amqpTemplate.convertAndSend("orderQueue", jsonObject);
        return orderId;
    }
}
